package com.example.front.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.common.dto.JsonResult;
import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.common.exception.BusinessException;
import com.example.front.dto.*;
import com.example.front.entity.Factory;
import com.example.front.serivce.WorkshopService;
import com.example.interceptor.UserContext;
import com.example.passport.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "严重等级")
@RestController
@CrossOrigin
@RequestMapping("/front/workshop")
public class WorkshopController {

    @Resource
    WorkshopService workshopService;

    @ApiOperation("添加车间")
    @PostMapping("/create")
    public JsonResult<String> create(@Valid WorkshopCreateDto workshopCreateDto) {

        workshopService.createWorkshop(workshopCreateDto);

        return new JsonResult<>(true, "添加成功");
    }

    @ApiOperation("列出车间列表")
    @GetMapping("/list")
    public JsonResult<ListDto<WorkshopListDto>> list(@Valid WorkshopSearchDto condition, Pagination pagination) {
        User user=UserContext.getUser();
        condition.setTenantId(UserContext.getUser().getTenantId());
        ListDto<WorkshopListDto> workshopDtoList = workshopService.listWorkshop(condition, pagination);
        return new JsonResult<>(workshopDtoList);
    }

    @ApiOperation("修改数据")
    @PostMapping("/update")
    public JsonResult<String> update(@Valid WorkshopDto workshopDto) {

        workshopService.update(workshopDto);
        return new JsonResult<>();
    }

    @ApiOperation("删除车间")
    @PostMapping("/delete")
    public JsonResult<String> delete(long id) {
        workshopService.deleteById(id,UserContext.getUser().getTenantId());
        return new JsonResult<>();
    }
}
